package com.lala.mq;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lala.dao.ClientOrderDao;
import com.lala.dao.OrderDetailsDao;
import com.lala.entity.ClientLogin;
import com.lala.entity.ClientNews;
import com.lala.entity.ClientOrder;
import com.lala.entity.OrderDetails;
import com.lala.service.ClientNewsService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * @author 霍明铭
 * @date 2020/3/29 17:36
 */
@Component
public class Consumer {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ClientNewsService newsService;
    @Autowired
    ClientOrderDao  orderDao;
    @Autowired
    OrderDetailsDao orderDetailsDao;



 //@RabbitListener(queues = "driver_order_queue_2")
    @RabbitHandler
    public void getNew(@Payload String ms){
     Map msg=new Gson().fromJson(ms,new TypeToken<Map<String,Map<String,String>>>() {}.getType());
     System.out.println(msg);
   Map driver=(Map)msg.get("driverInfo");
    Map driverOrder=(Map)msg.get("driverOrder");
     ClientOrder order= orderDao.getOrderByNumber(driverOrder.get("noOrder").toString());
     order.setStatus(1);
     orderDao.update(order);
     OrderDetails orderDetails= orderDetailsDao.queryById(order.getOrderDetailsId());
     orderDetails.setDriverId(Integer.parseInt(driver.get("id").toString()));
     orderDetailsDao.update(orderDetails);
     ClientNews news=new ClientNews();
     ClientLogin login=(ClientLogin)redisTemplate.opsForValue().get("user");
          news.setCreatTime(new Date());
          news.setDriverId(Integer.parseInt(driver.get("id").toString()));
          news.setIsRead("0");
          news.setStatus(0);
          news.setTitle("订单被接收");
          news.setUserId(login.getId());
     SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
          news.setContent("尊敬的"+login.getUserName()+"你好：您于"+ sf.format(news.getCreatTime()) +"创建的订单被"+driver.get("name")+"师傅接收 ");
           newsService.save(news);
    }


}
